package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pojo.PageBean;
import com.example.pojo.User;
import com.example.pojo.WorkPlan;
import com.example.service.WorkPlanService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/workPlan")
public class WorkPlanController {

    @Autowired
    private WorkPlanService workPlanService;

    @RequestMapping("/show.do")
    public String list(Model model, HttpSession session,Integer currentPage,Integer pageSize){
        if (currentPage==null) currentPage=1;
        if (pageSize==null) pageSize=2;

        /*获取登录用户*/
        User loginUser = (User) session.getAttribute("loginUser");
        /*查询登录用户所有计划*/
        QueryWrapper<WorkPlan> workPlanQueryWrapper = new QueryWrapper<>();
        workPlanQueryWrapper.eq("uid",loginUser.getUid());
        List<WorkPlan> workPlanList = workPlanService.list(workPlanQueryWrapper);
        /*获取现在时间，判断未完成状态的计划是否处于进行中（startTime-endTime),处于则修改状态码*/
        for (WorkPlan workPlan : workPlanList) {
            Date date = new Date();
            /*获取未完成的计划，并现在时刻在计划时间区间内*/
            if (date.compareTo(workPlan.getStartTime())==1&&date.compareTo(workPlan.getEndTime())==-1&&workPlan.getStatus()==0){
                /*修改状态码*/
                workPlan.setStatus(2);/*2代表计划进行中*/
                workPlanService.updateById(workPlan);
            }
        }
        /*获取更新后的计划列表*/
        PageHelper.startPage(currentPage,pageSize);
        List<WorkPlan> planList = workPlanService.list();

        // 分页
        PageInfo<WorkPlan> pageInfo = new PageInfo<>(planList);
        PageBean pageBean = new PageBean(currentPage, pageSize,(int)pageInfo.getTotal(), pageInfo.getList());
        model.addAttribute("workPlanList", planList);
        model.addAttribute("pageBean", pageBean);
        model.addAttribute("url", "/workPlan/show.do?");
        return "/jsp/workPlan/list.jsp";
    }

    /*跳转新建页面*/
    @RequestMapping("addUI.do")
    public String addUI(){

        return "/jsp/workPlan/add.jsp";
    }

    @RequestMapping("/addOrEdit.do")
    public String add(Long planId,String startTime,String endTime,String planName,String planContent,String description, HttpSession session){
        /*2022-09-01T12:12 返回的时间格式，需要转化*/
        String start = startTime.replaceAll("T", " ");
        String end = endTime.replaceAll("T", " ");
        /*获取登录用户*/
        User loginUser = (User) session.getAttribute("loginUser");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date startT = null;
        Date endT = null;
        try {
            startT = simpleDateFormat.parse(start);
            endT = simpleDateFormat.parse(end);

        } catch (ParseException e) {
        }
        /*封装保存*/
        if (planId!=null){/*修改计划*/
            WorkPlan newPlan = new WorkPlan
                    (planId,planName,loginUser.getUid().longValue(),description,planContent,startT,endT,0);
            workPlanService.updateById(newPlan);
            return "redirect:/workPlan/show.do";
        }
        /*新建计划*/
        WorkPlan workPlan = new WorkPlan
                (planName,loginUser.getUid().longValue(),description,planContent,startT,endT,0);
        workPlanService.save(workPlan);
        return "redirect:/workPlan/show.do";
    }

    /*跳转修改页面*/
    @RequestMapping("/editUI.do")
    public String editUI(Long planId,Model model){
        QueryWrapper<WorkPlan> workPlanQueryWrapper = new QueryWrapper<>();
        workPlanQueryWrapper.eq("planId",planId);
        WorkPlan workPlan = workPlanService.getOne(workPlanQueryWrapper);
        /*时间需要回显到页面 需要将时间转化为yyyy-MM-ddTHH:mm */
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String start = simpleDateFormat.format(workPlan.getStartTime());
        String end = simpleDateFormat.format(workPlan.getEndTime());
        String startT = start.replaceAll(" ", "T");
        String endT = end.replaceAll(" ", "T");
        workPlan.setStartT(startT);
        workPlan.setEndT(endT);
        model.addAttribute("workPlan",workPlan);
        return "/jsp/workPlan/edit.jsp";
    }

    /*删除计划*/
    @RequestMapping("/delete.do")
    public String delete(Long planId){
        workPlanService.removeById(planId);
        return "redirect:/workPlan/show.do";
    }

    /*修改状态码为完成状态 1*/
    @RequestMapping("/completed.do")
    public String completed(Long planId){
        QueryWrapper<WorkPlan> workPlanQueryWrapper = new QueryWrapper<>();
        workPlanQueryWrapper.eq("planId",planId);
        WorkPlan workPlan = workPlanService.getOne(workPlanQueryWrapper);
        workPlan.setStatus(1);
        workPlanService.updateById(workPlan);
        return "redirect:/workPlan/show.do";
    }


/*    public static void main(String[] args) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String s = "2022-12-12 12:12";
        String s1 = s.replaceAll(" ", "T");
        System.out.println(s1);


    }*/

}
